// player "class"

function Player(px, py) {

	//player variables
	this.velocityX = 0;
	this.velocityY = 0;
	
	this.posX = px;
	this.posY = py;
	
	this.thrusterX;
	this.thrusterY;
	this.friction = 0.997;
	
	this.radius = 25;
	
	this.collision=false;
	this.collisionOrbit=false;
	
	this.density = 0.000001;
	this.mass =  this.density * 4/3*Math.PI*Math.pow(this.radius, 3);   //can be 1
	this.gravity = 150;
	
	this.xGravity=0;
	this.yGravity=0;

	// handle input
	this.checkMouse = function(mx, my) {
		var dx = this.posX - mx;
		var dy = this.posY - my;
		var length = Math.sqrt(dx * dx + dy * dy);
				
		this.thrusterX = (dx / length) * 0.5;          //input sensitivity
		this.thrusterY = (dy / length) * 0.5;
				
		this.velocityX += this.thrusterX;
		this.velocityY += this.thrusterY;
	};
	
	// makes sure the player doesn't go out of bounds
	this.checkBoundaries = function() {
		if (this.posY > (3000 - this.radius) && this.velocityY > 0) {
			this.velocityY *= -1;
		}else if (this.posY < this.radius && this.velocityY < 0) {
			this.velocityY *= -1;
		};
		
		if (this.posX > (3000 - this.radius) && this.velocityX > 0) {
			this.velocityX *= -1;
		}else if (this.posX < this.radius && this.velocityX < 0) {
			this.velocityX *= -1;
		};
	};
	
	//checks for collision with other circles
	this.checkCollision = function(ball) {
		var dx = this.posX - ball.posX;
		var dy = this.posY - ball.posY;
		var radii = this.radius + ball.radius;
		this.collision=false;
		if (( dx * dx )  + ( dy * dy ) < radii * radii ) {			
			this.collision=true;
			this.velocityX*=-1;
			this.velocityY*=-1;
		}		
		
		var radii = this.radius + ball.radius*2.8;   //1.8 = orbit
		this.collisionOrbit=false;
		if (( dx * dx )  + ( dy * dy ) < radii * radii ) {			
			this.collisionOrbit=true;
			var distance=Math.sqrt(( dx * dx ) + ( dy * dy ));
			var force = this.gravity * (this.mass * ball.mass / (distance*distance));

			var xcomponent=dx/distance;
			var ycomponent=dy/distance;

			var deltav1=force/this.mass;
			if(isNaN(deltav1)) return;

			this.velocityX-=deltav1*xcomponent;
			this.velocityY-=deltav1*ycomponent;
		}	
	};
	
	// updates the player
    this.update = function() {
		this.checkBoundaries();
		
		this.velocityX *= this.friction;
		this.velocityY *= this.friction;
		
		this.posX += this.velocityX;
		this.posY += this.velocityY;
    };
    
    
	//draws the player
	this.draw = function(ctx, xView, yView){		
		// draw a simple rectangle shape as our player model
		ctx.fillStyle = "black";
		// before draw we need to convert player world's position to canvas position			
		ctx.beginPath();
		ctx.arc((this.posX) - xView, (this.posY) - yView, this.radius, 0, Math.PI * 2, true);
		ctx.closePath();
		ctx.fill();	
		
		/*ctx.fillText(this.velocityX, 300, 300);
		ctx.fillText(this.velocityY, 500, 300);
		
		ctx.fillText(this.posX, 300, 500);
		ctx.fillText(this.posY, 500, 500);*/
		
		if(this.collision==true)ctx.fillText("collision!", (this.posX-this.radius/2) - xView, (this.posY-this.radius/2) - yView);
		if(this.collisionOrbit==true)ctx.fillText("collision orbit!", (this.posX-this.radius/2) - xView, (this.posY+50-this.radius/2) - yView);
	};
};